package com.company.book_store.dao;

import com.company.book_store.pojo.Book;
import com.company.book_store.pojo.Bookinfo;
import com.company.book_store.util.jdbcutils;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class Bookinfoddao implements Ibookinfodao{
    private Connection con=null;
    @Override
    public int addbookinfo(Bookinfo bookinfo) {
        con= jdbcutils.getConnection();
        String sql="insert into book_info(username,book_name,date_borrow,date_return,book_id) values(?,?,?,?,?)";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,bookinfo.getUsername());
            pst.setString(2,bookinfo.getBook_name());
            pst.setDate(3,bookinfo.getDate_borrow());
            pst.setDate(4,bookinfo.getDate_return());
            pst.setInt(5,bookinfo.getBook_id());
            return pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Bookinfo> selectByusername(String username) {
        con= jdbcutils.getConnection();
        String sql="select * from book_info where username=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        List<Bookinfo>list=new ArrayList<>();
        try {
            pst=con.prepareStatement(sql);
            pst.setString(1,username);
            set=pst.executeQuery();
            while(set.next())
            {
                String username1=set.getString("username");
                String book_name=set.getString("book_name");
                Date date_borrow=set.getDate("date_borrow");
                Date date_return=set.getDate("date_return");
                int book_id=set.getInt("book_id");
                int id=set.getInt("id");
                Bookinfo bookinfo=new Bookinfo(username,book_name,date_borrow,date_return,book_id,id);
                list.add(bookinfo);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int deletebookinfo(int id,int book_id) {
        con= jdbcutils.getConnection();
        String sql="delete from book_info where id=? and book_id=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst=con.prepareStatement(sql);
            pst.setInt(1,id);
            pst.setInt(2,book_id);
            return pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Bookinfo selectByusernameandid(int id, int book_id) {
        con= jdbcutils.getConnection();
        String sql="select * from book_info where id=? and book_id=?";
        PreparedStatement pst=null;
        ResultSet set=null;
        try {
            pst=con.prepareStatement(sql);
            pst.setInt(1,id);
            pst.setInt(2,book_id);
            set=pst.executeQuery();
            if(set.next())
            {
                String username1=set.getString("username");
                String book_name=set.getString("book_name");
                Date date_borrow=set.getDate("date_borrow");
                Date date_return=set.getDate("date_return");
                int book_id1=set.getInt("book_id");
                Bookinfo bookinfo=new Bookinfo(username1,book_name,date_borrow,date_return,book_id,id);
                return bookinfo;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        Ibookinfodao ibookinfodao=new Bookinfoddao();
////        Calendar c=Calendar.getInstance();
////        c.add(Calendar.DATE,30);
////        Date date =new Date(System.currentTimeMillis());//借书时间
////        Date dd=new Date(c.getTimeInMillis());//还书时间
////        System.out.println(date);
////        System.out.println(dd);
////        Bookinfo bookinfo=new Bookinfo("张三","百年孤独",date,dd);
//        //ibookinfodao.addbookinfo(bookinfo);
////        List<Bookinfo>list=ibookinfodao.selectByusername("zhangsan");
////        for(Bookinfo bookinfo:list)
////        {
////            System.out.println(bookinfo);
////        }
//        System.out.println(ibookinfodao.addbookinfo(new Bookinfo("admin","hehe",null,null,5)));
//    }
}
